from playerCube import PlayerCube
from eventServer import eventServer
from gameServer import gameServer
from vec2 import Vec2

import random

class AICube(PlayerCube):
    def __init__(self, player):
        PlayerCube.__init__(self)
        
        self.defaultVar("player",player)
        self.defaultVar("playerType","computer")
        self.defaultVar("moveTo",Vec2(20,20))
        self.defaultVar("moveAccuracy",4)
        self.defaultVar("minJumpTime",0.25)
        self.defaultVar("maxJumpTime",1.5)
        self.defaultVar("minMoveOffsetTime",0.5)
        self.defaultVar("maxMoveOffsetTime",1.0)
        self.defaultVar("xThreshold",0.5)
        
        self.jumpTime = None
        self.jumpStartTime = None
        
        self.moveOffsetTime = None
        self.moveOffsetStartTime = None
        self.moveOffset = None
        self.paused = False
        
        eventServer.register("update",self.aiUpdate)
        eventServer.register("pause",self.pause)
        eventServer.register("unpause",self.unpause)
        
        #self.stopGoFast()
        
    def pause(self, cmd, args):
        if not args.has_key('bin') or args['bin']:
            self.paused = True
            
    def unpause(self, cmd, args):
        if not args.has_key('bin') or args['bin']:
            self.paused = False
        
    def aiUpdate(self, cmd, args):
        #PlayerCube.update(self,cmd,args)
        
        if self.getVar("dead") or self.paused:
            self.stopWalk()
            return
        
        time = args['time']
        dtime = args['dtime']
        
        if not self.jumpTime:
            self.jumpTime = random.uniform(self.getVar("minJumpTime"),self.getVar("maxJumpTime"))
        
        if not self.moveOffsetTime:
            minTime = self.getVar("minMoveOffsetTime")
            maxTime = self.getVar("maxMoveOffsetTime")
            self.moveOffsetTime = random.uniform(minTime,maxTime)
            
        if not self.moveOffsetStartTime or time - self.moveOffsetStartTime > self.moveOffsetTime:
            self.moveOffsetStartTime = time
            minTime = self.getVar("minMoveOffsetTime")
            maxTime = self.getVar("maxMoveOffsetTime")
            self.moveOffsetTime = random.uniform(minTime,maxTime)
            
            acc = self.getVar("moveAccuracy")
            self.moveOffset = Vec2(random.uniform(-acc,acc),random.uniform(-acc,acc))
        
        moveTo = self.getVar("moveTo") + self.moveOffset
        pos = self.getVar("pos")
        
        dx = moveTo.x - pos.x
        dy = moveTo.y - pos.y
        
        dx *= max(dy/10.0,1) ** 2.0
        
        if abs(dx) < self.getVar("xThreshold"):
            self.stopWalk()
        elif dx < 0:
            self.walk(True)
        else:
            self.walk(False)

        if not self.jumpStartTime:
            self.jumpStartTime = time
        elif time - self.jumpStartTime > self.jumpTime and self.onLand():
            self.jumpStartTime = time
            self.jumpTime = random.uniform(self.getVar("minJumpTime"),self.getVar("maxJumpTime"))
            
            if dy > 0:
                #print "jump"
                self.jump()
            else:
                #print "fall"
                self.fall()
            
